package plus.H5D3B5016.view;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import plus.H5D3B5016.R;

public class MyDialog extends DialogFragment {

    private Context   mContext;
    private View      frView;
    boolean fx=true;
    int je;
    EditText st;
    EditText et;
    Button ok;

    public MyDialog(Context context){
        this.mContext=context;
    }
    public MyDialog(Context context,boolean fx, int je){
        this.mContext=context;
        this.fx=fx;
        this.je=je;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        frView = inflater.inflate(R.layout.mydialog, null);
        st=frView.findViewById(R.id.st);
        et=frView.findViewById(R.id.et);
        ok=frView.findViewById(R.id.ok);

        if(fx){
            st.setText(""+(je-10));
            et.setText(""+(je));
        }else{
            st.setText(""+(je));
            et.setText(""+(je+10));
        }


        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(st.getText().toString().equals("")||et.getText().toString().equals("")){
                    Toast.makeText(mContext,"价格不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                int sts=Integer.valueOf(st.getText().toString());
                int ets=Integer.valueOf(et.getText().toString());
                if(sts>ets){
                    Toast.makeText(mContext,"区间设置有误",Toast.LENGTH_SHORT).show();return;
                }

                if(null!=mOnConfirmClickListener){
                    mOnConfirmClickListener.onConfirm(sts,ets);
                }
                dismiss();
            }
        });

        return frView;
    }


    public  interface OnConfirmClickListener {
        void onConfirm(int st,int et);
    }

    private OnConfirmClickListener mOnConfirmClickListener;
    public void setOnConfirmClickListener(OnConfirmClickListener listener) {
        mOnConfirmClickListener = listener;
    }

}
